package com.neu.his.service.registFee02.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neu.his.bo.regist.*;
import com.neu.his.core.Result;
import com.neu.his.entity.*;
import com.neu.his.mapper.*;
import com.neu.his.service.registFee02.RegistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.math.BigDecimal;
import java.util.*;

@Service
public class RegistImpl implements RegistService {
    @Autowired
    private RegistMapper registMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private RegistLevelMapper registLevelMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public Result RegistPost(RegistPostRec registPostRec){
        RegistPostSend registPostSend = new RegistPostSend();

        //获取患者id，不存在则插入
        QueryWrapper<Patient> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(Patient::getIdNumber,registPostRec.getPtIdNumber());
        List<Patient> list = patientMapper.selectList(queryWrapper);
        Long ptId;
        if(list.size() == 0){
            Patient patient = new Patient();
            patient.setAddress(registPostRec.getPtAddress());
            patient.setIdNumber(registPostRec.getPtIdNumber());
            patient.setName(registPostRec.getPtName());
            patient.setGender(registPostRec.getPtGender());
            patient.setBirthday(registPostRec.getPtBirthday());
            patientMapper.insert(patient);
            list = patientMapper.selectList(queryWrapper);
        }else{
            if(!list.get(0).getName() .equals(registPostRec.getPtName())){
                return Result.fail("姓名与身份证号不对应！");
            }
        }
        ptId = list.get(0).getId();
        BigDecimal totalCost = new BigDecimal(0);
        //获取部门id
        QueryWrapper<Department> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(Department::getDeptName,registPostRec.getDepartment());
        List<Department> departments = departmentMapper.selectList(queryWrapper1);
        Long deptId = departments.get(0).getId();

        //获取挂号等级id
        QueryWrapper<RegistLevel> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().eq(RegistLevel::getRegistLevel,registPostRec.getRegistLevel());
        List<RegistLevel> registLevels = registLevelMapper.selectList(queryWrapper2);
        Long levedId = registLevels.get(0).getId();
        totalCost = totalCost.add(registLevels.get(0).getRegistCost());
        //若需要病历
        boolean needBook = false;
        if(registPostRec.getNeedBook().equals( "true")) {
            BigDecimal tmp = new BigDecimal("1.0");
            totalCost = totalCost.add(tmp);
            needBook = true;
        }
        //查询是否需要医生
        Long doctorId = null;
        if(registPostRec.getDoctor() != null && registPostRec.getDoctor() != ""){
            QueryWrapper<User> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.lambda().eq(User::getName,registPostRec.getDoctor());
            List<User> users = userMapper.selectList(queryWrapper3);
            Long userId = users.get(0).getId();
            QueryWrapper<Doctor> queryWrapper4 = new QueryWrapper<>();
            queryWrapper4.eq("outpatient_doctor_id",userId);
            List<Doctor> doctors = doctorMapper.selectList(queryWrapper4);
            doctorId = doctors.get(0).getId();
        }
        Random r= new Random();
        Date date = new Date();
        Integer invoiceNumber = r.nextInt(10000);
        Regist regist = new Regist();
        regist.setPatientId(ptId);
        regist.setCreateDate(date);
        regist.setRegistDate(date);
        regist.setPatientId(ptId);
        regist.setDoctorId(doctorId);
        regist.setDepartmentId(deptId);
        regist.setTotalCost(totalCost);
        regist.setRegistLevel(levedId);
        regist.setNeedBook(needBook);
        regist.setInvoiceNumber(invoiceNumber.toString());
        regist.setPaid(true);
        registMapper.insert(regist);
        Long id = regist.getId();
        registPostSend.setPatientId(ptId);
        registPostSend.setTotalCost(totalCost);
        registPostSend.setInvoiceNumber(invoiceNumber.toString());
        return Result.success(registPostSend);
    }

    @Override
    public Result RegistPostDept(RegistDept registDept){
        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(Department::getDeptName,registDept.getDept());
        Long id = departmentMapper.selectList(queryWrapper).get(0).getId();
        List<Department> departments = departmentMapper.getDepartmentUser(id);
        List<String> doctors = new ArrayList<>();
        for(Department department:departments){
            for(User user:department.getUserList()){
                doctors.add(user.getName());
            }
        }
        RegistGetSend registGetSend = new RegistGetSend();
        registGetSend.setDoctorArr(doctors);
        return Result.success(registGetSend);
    }

    @Override
    public Result RegistGet(){
        List<String> registLevel = new ArrayList<>();
        List<String> dept = new ArrayList<>();
        List<String> doctor = new ArrayList<>();

        //获取挂号等级
        QueryWrapper<RegistLevel> queryWrapper = new QueryWrapper<>();
        List<RegistLevel>  registLevels  = registLevelMapper.selectList(queryWrapper);
        Set<String> s1 = new HashSet<>();
        for(RegistLevel registLevel1:registLevels){
            s1.add(registLevel1.getRegistLevel()) ;
        }
        for(String s:s1){
            registLevel.add(s);
        }
        s1.clear();
        QueryWrapper<Department> queryWrapper2 = new QueryWrapper<>();
        List<Department>  departments  = departmentMapper.selectList(queryWrapper2);
        for(Department department:departments){
            s1.add(department.getDeptName()) ;
        }
        for(String s:s1){
            dept.add(s);
        }

//        s1.clear();
//        List<Doctor> doctors = doctorMapper.getDoctorUser(-1L);
//        for(Doctor doctor1:doctors){
//            s1.add(doctor1.getUser().getName()) ;
//        }
//        for(String s:s1){
//            doctor.add(s);
//        }
        RegistGetSend registGetSend = new RegistGetSend();
        registGetSend.setRegistLevelArr(registLevel);
        registGetSend.setDepartmentArr(dept);
//        registGetSend.setDoctorArr(doctor);
        return Result.success(registGetSend);
    }

    @Override
    public Result RegistPay(RegistPay registPay){
        Regist regist = registMapper.selectById(registPay.getId());
        regist.setPaid(true);
        registMapper.updateById(regist);
        return Result.success();
    }
}
